﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Microsoft.AspNetCore.Mvc;
using shenghua.Authorization;
using shenghua.Basics.CommodityClasses;
using shenghua.Basics.CommodityClassSetting.Dto;
using shenghua.Basics.Setting.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Basics.CommodityClassSetting
{
    [AbpAuthorize(PermissionNames.CommodityClassManagement)]
    public class CommodityClassAppService: shenghuaAppServiceBase, ICommodityClassAppService
    {
        private readonly CommodityClassManager _commodityClassManager;

        public  CommodityClassAppService(
            CommodityClassManager commodityClassManager)
        {
            _commodityClassManager = commodityClassManager;
        }

        public async Task<CommodityClassDto> Create(CreateCommodityClassDto input)
        {
            CommodityClass commodityClass = new CommodityClass
            {
                ParentId = input.parentId,
                Name = input.Name,
                DisplayName = input.DisplayName
            };
            await _commodityClassManager.Add(commodityClass);
            return ObjectMapper.Map<CommodityClassDto>(commodityClass);
        }

        public async Task<CommodityClassDto> Update(UpdateCommodityClassDto input)
        {
            CommodityClass commodityClass = await _commodityClassManager.Get(input.Id);
            commodityClass.DisplayName = input.DisplayName;
            await _commodityClassManager.Update(commodityClass);
            return ObjectMapper.Map<CommodityClassDto>(commodityClass);
        }

        [HttpPost]
        public async Task Delete(EntityDto input)
        {
            await _commodityClassManager.Delete(input.Id, true);
        }

        public async Task<List<CommodityClassDto>> GetAll(int? parentId)
        {
            List<CommodityClass> commodityClasses = await _commodityClassManager.GetAll(parentId);
            return ObjectMapper.Map<List<CommodityClassDto>>(commodityClasses);
        }

        public async Task<List<WordTreeDto<CommodityClassDto>>> GetTree(int? parentId)
        {
            return ObjectMapper.Map<List<WordTreeDto<CommodityClassDto>>>(await _commodityClassManager.GetTree());
        }

        public async Task SortUp(EntityDto input)
        {
            await _commodityClassManager.SortUp(input.Id);
        }

        public async Task SortDown(EntityDto input)
        {
            await _commodityClassManager.SortDown(input.Id);
        }

        public async Task MoveTo(MoveCommodityClassToDto input)
        {
            await _commodityClassManager.MoveTo(input.Id, input.ParentId);
        }

        public async Task MoveUp(EntityDto input)
        {
            await _commodityClassManager.MoveUp(input.Id);
        }

        public async Task MoveDown(EntityDto input)
        {
            await _commodityClassManager.MoveDown(input.Id);
        }
    }
}
